package com.ruoyi.bus.wechat;

import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.system.service.ISysConfigService;
import lombok.extern.slf4j.Slf4j;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;

/**
 * @Description 微信工具类
 * @Author caiyanshan
 * @Date 2021/4/21 8:40
 * @Param
 * @Return
 */
@Slf4j
public class WechatUtils {
  private static RedisCache redisCache;
  
  private static ISysConfigService configService;

  private static RedisCache getRedisCache(){
    if(redisCache == null){
      redisCache = SpringUtils.getBean(RedisCache.class);
    }
    return redisCache;
  }
  
  private static ISysConfigService getConfigService(){
    if(configService == null){
      configService = SpringUtils.getBean(ISysConfigService.class);
    }
    return configService;
  }
  
  private static String key = "wechat:";
  
  /**
   * @Description  1是司机  2是用户
   * @Author caiyanshan
   * @Date 2021/4/23 14:55
   * @Param [type]
   * @Return com.ruoyi.bus.wechat.Wechat
   */
  public static Wechat getWechat(Integer type){
    Wechat wechat = getRedisCache().getCacheObject(key + type);
    if(wechat == null){
      String appId = getConfigService().selectConfigByKey("wechat.appId." + type);
      String appSecret = getConfigService().selectConfigByKey("wechat.appSecret." + type);
      wechat = new Wechat();
      wechat.setAppId(appId);
      wechat.setAppSecret(appSecret);
      setWechat(wechat, type);
    }
    return wechat;
  }
  
  public static void setWechat(Wechat wechat, Integer type){
    getRedisCache().setCacheObject(key + type, wechat, 24, TimeUnit.HOURS);//24小时过期
  }
  
  public static void removeWechat(Integer type){
    getRedisCache().deleteObject(key + type);
  }
  
  /**
   * @Description 签名
   * @Author caiyanshan
   * @Date 2021/4/21 9:01
   * @Param [params, urlEncoder]
   * @Return java.lang.String
   */
  public static String packageSign(Map<String, String> params, boolean urlEncoder) {
    TreeMap<String, String> sortedParams = new TreeMap(params);
    StringBuilder sb = new StringBuilder();
    boolean first = true;
    Iterator var5 = sortedParams.entrySet().iterator();
    
    while(var5.hasNext()) {
      Map.Entry<String, String> param = (Map.Entry)var5.next();
      String value = (String)param.getValue();
      if (!StringUtils.isBlank(value)) {
        if (first) {
          first = false;
        } else {
          sb.append("&");
        }
        
        sb.append((String)param.getKey()).append("=");
        if (urlEncoder) {
          try {
            value = urlEncode(value);
          } catch (UnsupportedEncodingException var9) {
          }
        }
        
        sb.append(value);
      }
    }
    
    return sb.toString();
  }
  
  public static String urlEncode(String src) throws UnsupportedEncodingException {
    return URLEncoder.encode(src, Charsets.UTF_8.name()).replace("+", "%20");
  }
  
}
